﻿Imports Training.Common

Partial Public Class requestAccount
    Inherits System.Web.UI.Page
    Dim blnAC As Boolean ' account creation

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    ''' <summary>
    ''' Complete Button Click Event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Check validation before adding user</remarks>
    Private Sub btnComplete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComplete.Click
        If Page.IsValid Then
            CreateUser()
        Else
            lblAlert.Text = "All required field were not complete or a validation error occurred."
        End If
    End Sub

    ''' <summary>
    ''' Create membership login and add to Users table
    ''' </summary>
    ''' <remarks>Checks prior to this function should prevent errors</remarks>
    Private Sub CreateUser()
        Dim retVal As String
        Dim uCount As Integer
        Dim cPass As String
        Dim eMsg As String

        ' check email for unique
        'If CheckMemberEmail(tbEmail.Text) > 0 Then
        '    lblAlert.Text = " Email address is in use, please enter a different Email address."
        '    Exit Sub
        'End If

        ' check for unique login
        If CheckLoginName(tbLogin.Text) > 0 Then
            lblAlert.Text = " Login name is in use, please enter a different login name. "
            Exit Sub
        End If

        ' generate password
        cPass = Membership.GeneratePassword(6, 0)

        ' create membership account
        retVal = AddGroupLogin(tbLogin.Text, cPass, tbEmail.Text)

        ' add user to "user" role
        Roles.AddUserToRole(tbLogin.Text, "user")

        ' lookup userid for user to associate login with user table
        Dim user As MembershipUser = Membership.GetUser(tbLogin.Text)
        Dim uid As String = user.ProviderUserKey.ToString()

        ' add user and get iError code
        uCount = AddGroupUser(uid, tbFname.Text, tbLname.Text, 1, tbCompany.Text, tbAddress.Text, tbCity.Text, _
                              ddlState.SelectedValue, tbZip.Text, tbPhone.Text, _
                              tbEmail.Text, "", 1)
        If uCount > 0 Then
            lblAlert.Text = " Error adding User. Error has been logged."
        Else
            lblResult.Text = " Your individual account is now active. Return to the login screen, check your email for your login credentials."
            msg.Visible = True
            main.Visible = False
            ' send email notification of new account credentials
            eMsg = tbFname.Text & " " & tbLname.Text & " your Training account has been created.<br /> " & _
                "Username: " & tbLogin.Text & "<br />" & _
                "Password: " & cPass & "<br />" & _
                "If you have any issues logging in, please contact webmaster@truedetectives.com"

            SendEmail(tbEmail.Text, "Training Account Creation", eMsg)

            eMsg = "A new account for Training has been generated for " & tbFname.Text & " " & tbLname.Text & _
                "<br />The account is active and login credentials have been emailed to them."
            SendEmail("yourname@yourdomain.com", "Training Account Creation", eMsg)
        End If

    End Sub
End Class